# arthor vuong.quoc
#! C:/Perl/bin/perl -w
use strict;
use warnings;
use Juniper;
#use autodie;
my @data;
sub readpara
{	

	my @fileName;
	#print "Input path : ";
	my $path = "D:/par";#<STDIN>;
	chomp($path);
	opendir (DIR, $path) or die $!;
	while(my $file = readdir(DIR))
	{
		if($file !~ m/^\./)
		{
			push(@fileName,$file);
		}
	}                                      
	closedir DIR;
	foreach my $file (@fileName)
	{
		my $topology;
		my $variable;
		my $testcase;
		my $routerConfig;
		open File, ("+<$path"."/"."$file") or die $!;
		my @list = <File>;
		my $size = @list;
		my $i;
		my $flag = 0;
		my $row ="";
		for($i=0;$i<$size;$i++)
		{	
			
			# remove space & comment
			$list[$i] = &removeComment($list[$i]);
			# Read file to obj
			if($list[$i] =~ m/x-test-topology/)
			{
				while($list[$i] !~ m/\;/)
				{
					$list[$i] = &removeComment($list[$i]);
					$topology = $topology."$list[$i]";
					$i++;
				}
				$topology = $topology."$list[$i]";
				
			}
			elsif($list[$i] =~ m/x-testcase-/)
			{
				while($list[$i] !~ m/\;/)
				{
					$list[$i] = &removeComment($list[$i]);
					$testcase = $testcase."$list[$i]";
					$i++;
				}
				$testcase = $testcase."$list[$i]";
				
			}
			elsif($list[$i] =~ m/x-/ && $list[$i] !~ m/x-testcase-/ && $list[$i] !~ m/x-test-topology/)	
			{
				while($list[$i] !~ m/\;/)
				{
					$list[$i] = &removeComment($list[$i]);
					$variable = $variable."$list[$i]";
					$i++;
				}
				$variable = $variable."$list[$i]";
			}
			elsif($list[$i] =~ m/^r/ || $list[$i] =~ m/^h/)
			{
				#print $list[$i];
				my $close = 0 ;
				my $open = 0;
				my $flag = 0;
				do
				{
					$list[$i] = &removeComment($list[$i]);
					$routerConfig = $routerConfig."$list[$i]";
					my @countopen = $list[$i] =~ /\}/g;
					my @countclose = $list[$i] =~ /\{/g;
					$open = $open + scalar @countopen;
					$close = $close + scalar @countclose;
					if($open > 0 && $close > 0 && $open == $close)
					{
						$flag = 1 ;
						$routerConfig = $routerConfig."\nEND NOW\n";							
					}
					$i++;
				}while($flag < 1 );	
			}			
		}
		# get data
		my $object = new Juniper($file, $variable, $testcase, $topology, $routerConfig);
		push(@data, $object);
		# Destroy
	}
	print "Size: ",scalar @data,"\n";
	# print $data[20]->showAll();
}
# show all data 
sub ShowAllData
{
	foreach my $file (@data)
	{
		print $file->getRouterConfig();
	}
}
# Remove all Space in first and in the end.
sub regex_or {
  my $string = shift;
  $string =~ s/(?:^\s+)||(?: +$)//g;
  return $string;
}
sub for_plus {
	my $string = shift;
	for ($string) { s/^\s*//; s/\s*$//; }
	return $string;
}
sub double_star {
	my $string = shift;
	$string =~ s/^\s*//;
	$string =~ s/\s*$//;
	return $string;
}
# Remove all space and comment
sub removeComment
{
  my $string = shift;
  $string =regex_or($string);
  my @results = split("#",$string);
  return $results[0];

}
#function reardpara()
&readpara();
&ShowAllData();


my $stop = <STDIN>;
